﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities.QuickSql;
using MySql.Data;
using System.Threading;

namespace Deviseur_Cs2i_Car
{
    public partial class AddOption : Form
    {
        private OPTION Option;

        public AddOption()
        {
            InitializeComponent();

            this.Text = "Ajouter une option";
            this.buttonAddOption.Text = "Ajouter";

            DataTable DataCategOption = new DataTable();

            DataCategOption.Columns.Add("NumCateg");
            DataCategOption.Columns.Add("LibelleCateg");
            DataCategOption.Rows.Add("0", "- Choisir une categorie -");

            Table categ = Program.Db.QueryTable("SELECT * FROM CATEGOPTION");
            foreach (Record r in categ)
                DataCategOption.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxCategOption.DataSource = DataCategOption;
            this.comboBoxCategOption.ValueMember = "Numcateg";
            this.comboBoxCategOption.DisplayMember = "LibelleCateg";

            this.comboBoxCategOption.SelectedIndex = 0;
        }
        public AddOption(int NumOption)
        {
            InitializeComponent();
            
            this.Text = "Modifier une option";
            this.buttonAddOption.Text = "Modifier";

            DataTable DataCategOption = new DataTable();

            DataCategOption.Columns.Add("NumCateg");
            DataCategOption.Columns.Add("LibelleCateg");
            DataCategOption.Rows.Add("0", "- Choisir une categorie -");

            Table categ = Program.Db.QueryTable("SELECT * FROM CATEGOPTION");
            foreach (Record r in categ)
                DataCategOption.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxCategOption.DataSource = DataCategOption;
            this.comboBoxCategOption.ValueMember = "Numcateg";
            this.comboBoxCategOption.DisplayMember = "LibelleCateg";

            this.comboBoxCategOption.SelectedIndex = 0;

            Table TypeVoiture2 = Program.Db.QueryTable("SELECT * FROM OPTIONS WHERE NumOption = " + NumOption);
            foreach (Record r in TypeVoiture2)
            {
                this.textlibelleOption.Text = r.Values[1].ToString();
                this.textPrixOption.Text = r.Values[2].ToString();
                this.comboBoxCategOption.SelectedValue = r.Values[3].ToString();
                Option = new OPTION(NumOption, this.textlibelleOption.Text.ToString(), Convert.ToDouble(this.textPrixOption.Text.ToString()), Convert.ToInt32(this.comboBoxCategOption.SelectedValue.ToString()));
            }
        }


        private void buttonAddOption_Click(object sender, EventArgs e)
        {
            if (textlibelleOption.Text.ToString() != "" && textPrixOption.Text.ToString() != "" && comboBoxCategOption.SelectedValue.ToString() != "0")
            {
                if (this.buttonAddOption.Text == "Ajouter")
                {
                    if (textPrixOption.Text == "")
                        textPrixOption.Text = "0";

                    Option = new OPTION(textlibelleOption.Text.ToString(), Convert.ToDouble(textPrixOption.Text.ToString()), Convert.ToInt32(this.comboBoxCategOption.SelectedValue.ToString()));

                    Thread addThread = new Thread(new ThreadStart(addOption_Thread));
                    addThread.Start();
                }
                else if (this.buttonAddOption.Text == "Modifier")
                {
                    Option.set_LibelleOption(this.textlibelleOption.Text.ToString());
                    Option.set_PrixOption(Convert.ToDouble(textPrixOption.Text.ToString()));
                    Option.set_CategOption(new CATEGOPTION(Convert.ToInt32(this.comboBoxCategOption.SelectedValue.ToString())));
                    Thread updateThread = new Thread(new ThreadStart(updateOption_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
            
        }
        private void addOption_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Option.new_Option();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void updateOption_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Option.modify_Option();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void Disabled()
        {
            this.textlibelleOption.Enabled = false;
            this.buttonAddOption.Enabled = false;
            this.textPrixOption.Enabled = false;
            this.comboBoxCategOption.Enabled = false;
        }
        
        private void CloseFen()
        {
            this.Close();
        }
    }
}